package cn.enjoy.tool.utils.select;

import java.util.function.Function;
import java.util.function.Predicate;

/**
 * 分支
 *
 * @author K
 * @date 2020/12/16 11:36
 */
public interface Branch<P, T> {

    /**
     * 判断
     *
     * @return Predicate<P>
     */
    Predicate<P> tester();

    /**
     * 值计算
     *
     * @return Function<P, T>
     */
    Function<P, T> factory();

    /**
     * 构造tester、cn.enjoy.design.create.factory
     *
     * @param tester  判断
     * @param factory 值计算
     * @param <P>     参数
     * @param <T>     返回值
     * @return Branch<P, T>
     */
    static <P, T> Branch<P, T> of(Predicate<P> tester, Function<P, T> factory) {
        return new Branch<P, T>() {
            @Override
            public Predicate<P> tester() {
                return tester;
            }

            @Override
            public Function<P, T> factory() {
                return factory;
            }
        };
    }

}
